﻿using System;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Input;
using MyFloridaHouse.libSvOpenDistricts.MapShapes;
using Microsoft.Maps.MapControl;

namespace MyFloridaHouse.SilverlightOpenDistricts
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }


        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
        	try
        	{
				
				//This construct allows us to avoid a chain of errors if the ViewModel fails to initialize.
				if(InitializeViewModel())
				{
					InitializeEvents();		// this will fail if the viewmodel fails initialization
					InitializePropertyChangedListener(); 
					InitializeDiaglogs();	// this will leave all dialogs on the screen if the viewmodel fails initialization
				}
        	}
        	catch (Exception exception)
        	{
        		MessageBox.Show(
        			"Failed to properly initialize. Possible cause is connection failure to data." + exception.Message,
        			"Failed Initialization", MessageBoxButton.OK);
        	}
        }

        private void btn_FindPopup_Click(object sender, RoutedEventArgs e)
        {
            findPopUp.Visibility = Visibility.Visible;
        }

        private void MyMap_MouseClick(object sender, MapMouseEventArgs e)
        {
            Map map = (Map)sender;
            Location location = map.ViewportPointToLocation(e.ViewportPoint);
            viewModelBase.mapMouseLatLon = location;
        }

        private void MyMap_MouseDoubleClick(object sender, MapMouseEventArgs e)
        {
            Map map = (Map)sender;
            Location location = map.ViewportPointToLocation(e.ViewportPoint);
            viewModelBase.mapMouseLatLon = location;

            //prevent default bingmaps behavior of double click zooming map
            e.Handled = true;
        }

        private void MyMap_ViewChangeEnd(object sender, MapEventArgs e)
        {
            Map map = (Map)sender;
            viewModelBase.mapLatLonNW = map.BoundingRectangle.Northwest;
            viewModelBase.mapLatLonSE = map.BoundingRectangle.Southeast;
            viewModelBase.mapViewPortNW = map.LocationToViewportPoint(map.BoundingRectangle.Northwest);
            viewModelBase.mapViewPortSE = map.LocationToViewportPoint(map.BoundingRectangle.Southeast);

            if (viewModelBase.mapPanControl.IsViewOutsideLimits(viewModelBase.mapLatLonNW, viewModelBase.mapLatLonSE))
            {
                viewModelBase.mapCenter = viewModelBase.mapPanControl.mapCenterDefault;
            }
            //having trouble with binding relative elements, so manually set it
            MyMiniMap.Center = MyXamlMainMap.Center;
        }



        private void cmbBx_LayerColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cmb = (ComboBox)sender;
            if (cmb != null)
            {
                MapShapeLayer localMapLayer = (MapShapeLayer)cmb.Tag;
                //todo: change explicit cast of string to dynamically discovered type
                localMapLayer.layerColor = (string)cmb.SelectedItem;
            }
            viewModelBase.MapDataRefresh();  //need to show layers whenever show option is changed
        }


        // todo: move this to view model by keeping the last keystroke as a property there
        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == EASY_CLICK_KEY)
            {
                viewModelBase.attemptToggleEasyClickMode();
            }
            else if (e.Key == DATA_FREEZE_KEY)
            {
                viewModelBase.attemptToggleFreezeDataMode();
            }
        }

        private void btn_GoToFullScreen_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
            if (Application.Current.Host.Content.IsFullScreen)
            {
                btn_FullScreen.Content = "Exit Full Screen";
            }
            else
            {
                btn_FullScreen.Content = "Full Screen";
            }

        }

        private void btn_Home_Click(object sender, RoutedEventArgs e)
        {
            HtmlPage.Window.Navigate(new Uri("http://www.myfloridacensus.gov", UriKind.Absolute));
        }

        private void btn_HelpQuickStart_Click(object sender, RoutedEventArgs e)
        {
            HtmlPage.Window.Navigate(new Uri("http://censusvalidator.blob.core.windows.net/mydistrictbuilderdata/Public Participation/MyDistrictBuilder_Quick_Start.pdf"), "_blank");
        }

        private void btn_HelpTutorial_Click(object sender, RoutedEventArgs e)
        {
            HtmlPage.Window.Navigate(new Uri("http://censusvalidator.blob.core.windows.net/mydistrictbuilderdata/Public Participation/MyDistrictBuilder_Tutorial.pdf"), "_blank");
        }

        private void btn_HelpTutorialVideos_Click(object sender, RoutedEventArgs e)
        {
            HtmlPage.Window.Navigate(new Uri("http://www.youtube.com/MyDistrictBuilderFL"), "_blank");
        }

        private void btn_HelpGlossary_Click(object sender, RoutedEventArgs e)
        {
            HtmlPage.Window.Navigate(new Uri("http://censusvalidator.blob.core.windows.net/mydistrictbuilderdata/Public Participation/MyDistrictBuilder_Glossary.pdf"), "_blank");
        }

        private void btn_Email_Click(object sender, RoutedEventArgs e)
        {
            HtmlPage.Window.Navigate(new Uri("mailto:mydistrictbuilder@myfloridahouse.gov"), "_blank");
        }

        private void btn_Facebook_Click(object sender, RoutedEventArgs e)
        {
            HtmlPage.Window.Navigate(new Uri("http://www.facebook.com/MyDistrictBuilder"), "_blank");
        }

        private void btn_Twitter_Click(object sender, RoutedEventArgs e)
        {
            HtmlPage.Window.Navigate(new Uri("http://twitter.com/FLRedistricting#"), "_blank");
        }

        private void btn_YouTube_Click(object sender, RoutedEventArgs e)
        {
            HtmlPage.Window.Navigate(new Uri("http://www.youtube.com/user/MyDistrictBuilderFL"), "_blank");
        }

        private void btn_MassDataReport_Click(object sender, RoutedEventArgs e)               
        {
            HtmlPage.Window.Navigate(new Uri("MyDistrictBuilderMassDataReport.html", UriKind.Relative), "_blank");
        }
    }
}
